﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using ESRI.ArcGIS.ADF.BaseClasses;
using PlanningGIS.Framework.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesFile;
using PlanningGIS.Framework;
using DevComponents.DotNetBar;
using System.Windows.Forms;
using PlanningGIS.Engine.Controls;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    /// 作用：加载数据
    /// </summary>
    public class AddData : BaseCommand
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public AddData()
        {
            base.m_caption = "加载数据";
            base.m_toolTip = base.Caption;
        }

        /// <summary>
        /// 应用程序
        /// </summary>
        IApplication m_Application = null;
        
        /// <summary>
        /// 右键图层
        /// </summary>
        ICompositeLayer pCompositeLayer = null;

        /// <summary>
        /// 创建按钮
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
            }
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        public override void OnClick()
        {
            if (m_Application == null) return;
            ILayer pLayer = (m_Application as IFramework).TOCControl.CustomProperty as ILayer;
            if (pLayer != null && pLayer is ICompositeLayer)
                pCompositeLayer = pLayer as ICompositeLayer;
            else
                pCompositeLayer = null;

            OpenDataDialog openDataDialog = new OpenDataDialog();
            openDataDialog.MultiSelect = true;
            if (openDataDialog.ShowDialog((m_Application as IFramework).MdiForm) != DialogResult.OK) return;
            try
            {
                openDataDialog.Cursor = Cursors.WaitCursor;
                List<ILayer> pLayers = openDataDialog.GetSelectLayers(false);
                if (pLayers == null || pLayers.Count == 0) return;
                IEnvelope env = null;
                foreach (ILayer lyr in pLayers)
                {
                    SetRasterLayerStretch(lyr);
                    if (openDataDialog.IsZoom)
                    {
                        if (env == null)
                            env = lyr.AreaOfInterest;
                        else
                            env.Union(lyr.AreaOfInterest);
                    }
                    
                    if (pCompositeLayer != null)
                        (pCompositeLayer as IGroupLayer).Add(lyr);
                    else
                        (m_Application.Document as IMxDocument).AddLayer(lyr);
                }
                if (openDataDialog.IsZoom)
                    (m_Application as IFramework).MapControl.Extent = env;
                (m_Application as IFramework).TOCControl.Update();
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox("加载数据失败：" + ex.Message);
            }
            finally
            {
                openDataDialog.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 影像图层设置默认不拉伸
        /// </summary>
        /// <param name="pLayer"></param>
        private void SetRasterLayerStretch(ILayer pLayer)
        {
            if (!(pLayer is IRasterLayer)) return;
            IRasterLayer pRasterLayer = pLayer as IRasterLayer;
            IRasterRenderer pRasterRGBRenderer = pRasterLayer.Renderer;
            if (pRasterRGBRenderer == null || !(pRasterRGBRenderer is IRasterRGBRenderer)) return;
            IRasterStretch2 pRasterStretch = pRasterRGBRenderer as IRasterStretch2;
            pRasterStretch.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_NONE;
            IRasterStretch3 pRasterStretch3 = pRasterRGBRenderer as IRasterStretch3;
            pRasterStretch3.UseGamma = false;
            pRasterRGBRenderer.Update();
        }

        /// <summary>
        /// 是否可用
        /// </summary>
        public override bool Enabled
        {
            get
            {
                if (m_Application == null) return false;
                return (m_Application as IFramework).IsMapCtrlactive;
            }
        }

    }

}
